
clear
do "...\First.do"

********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2

keep if N_pre_2==5

drop if timing>3
drop if timing<-3


********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post

label var help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"



*** DETECTION VS. ADHERENCE
g pre_statins=(statins==1 & post==0)
g pre_ACE=(ACE==1 & post==0)
g pre_diabetes=((diabetes_control==1 | metformin==1) & post==0)
g pre_copd=((ACSC_COPD==1 | COPD_med==1) & post==0)


foreach var of varlist pre_statins pre_ACE pre_diabetes pre_copd {
	
 bys pnr: egen d_`var'=max(`var')
	drop `var'
	
}

g d_pre_CVC=(d_pre_statins==1 | d_pre_ACE==1)

cap drop any
egen any=rowmax(d_pre_statins d_pre_diabetes d_pre_copd)



********************************************************************************
********************************************************************************


* Overall effect

* STATINS
sum statins if post==0 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum statins if post==0 & low_ses==1 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest statins if post==1 & help3==0  & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe statins post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X if male==1, a(id year) cluster(id) 

outreg2 using "$table\TableD7_panelA.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Statins)


* COPD

sum ACSC_COPD if post==0 , d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACSC_COPD if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest ACSC_COPD if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACSC_COPD post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, cluster(id year) a(id)

outreg2 using "$table\TableD7_panelA.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(COPD Hospitalization)

* DIABETES

sum diabetes_control if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum diabetes_control if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest diabetes_control if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe diabetes_control post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id) 

outreg2 using "$table\TableD7_panelA.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Diabetes Checkup)



* LUNG SCANS
sum lungscan if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum lungscan if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest lungscan if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe lungscan post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X if male==0, cluster(id year) a(id) 

outreg2 using "$table\TableD7_panelA.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Lung scans)






********************************************************************************
** ADHERENCE 
********************************************************************************

* STATINS
sum statins if d_pre_CVC==1 & post==0 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum statins if post==0 & low_ses==1 & d_pre_CVC==1 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest statins if post==1 & help3==0 & d_pre_CVC==1 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe statins post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X if d_pre_CVC==1 &  male==1, cluster(id) a(id year)  

outreg2 using "$table\TableD7_panelC.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Statins)




* ACSC COPD
sum ACSC_COPD if d_pre_copd==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACSC_COPD if post==0 & low_ses==1 & d_pre_copd==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest ACSC_COPD if post==1 & help3==0 & d_pre_copd==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACSC_COPD post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if d_pre_copd==1, cluster(id) a(id year) 

outreg2 using "$table\TableD7_panelC.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(COPD Hospitalization)



* DIABETES
sum diabetes_control if d_pre_diabetes==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum diabetes_control if post==0 & low_ses==1 & d_pre_diabetes==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest diabetes_control if post==1 & help3==0 & d_pre_diabetes==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe diabetes_control post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X if d_pre_diabetes==1, cluster(id) a(id year) 

outreg2 using "$table\TableD7_panelC.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Diabetes Checkup)





********************************************************************************
* DETECTION
********************************************************************************

* STATINS
sum statins if d_pre_CVC==0 & post==0 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum statins if post==0 & low_ses==1 & d_pre_CVC==0 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 


ttest statins if post==1 & help3==0 & d_pre_CVC==0 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe statins post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X if  d_pre_CVC==0 & male==1, cluster(id) a(id year)

outreg2 using "$table\TableD7_panelB.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Statins) 




* ACSC COPD
sum ACSC_COPD if d_pre_copd==0 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACSC_COPD if post==0 & low_ses==1 & d_pre_copd==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest ACSC_COPD if post==1 & help3==0 & d_pre_copd==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACSC_COPD post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if  d_pre_copd==0, cluster(id) a(id year) 

outreg2 using "$table\TableD7_panelB.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(COPD Hospitalization) 





* DIABETES

sum diabetes_control if d_pre_diabetes==0 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum diabetes_control if post==0 & low_ses==1 & d_pre_diabetes==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest diabetes_control if post==1 & help3==0 & d_pre_diabetes==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe diabetes_control post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if  d_pre_diabetes==0, cluster(id) a(id year)

outreg2 using "$table\TableD7_panelB.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Diabetes Checkup)



* LUNG SCANS
sum first_lung if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum first_lung if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest first_lung if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe first_lung post help3 low_ses interaction interaction2 interaction3 ddd3 i.age male married non_dk if  male==0, cluster(new_gp) a(gp_fe year)

outreg2 using "$table\TableD7_panelB.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Lung scans)



*******************************************************************************
* Other drugs - Table D6
*******************************************************************************


* ACE
sum ACE if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACE if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 


ttest ACE if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACE post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, cluster(id) a(id year)

outreg2 using "$table\TableD6.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(ACE inhibitors) 


* Metformin
sum metformin if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum metformin if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest metformin if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe metformin post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, cluster(id) a(id year)  

outreg2 using "$table\TableD6.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Metformin)

* COPD medication
sum COPD_med if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum COPD_med if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest COPD_med if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe COPD_med post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, cluster(id) a(id year) 

outreg2 using "$table\TableD6.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(COPD Medication)




*******************************************************************************
* Dectection/Adhence on mortality 
** Table D8
*******************************************************************************

sum death if post==1 & any==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & any==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & any==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & any==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD8_PanelA.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(All-cause Mortality)



sum death_CVC if post==1 & d_pre_CVC==0 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if post==1 & low_ses==1 & d_pre_CVC==0 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help3==0 & d_pre_CVC==0 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC help3 low_ses interaction2 $patient_X $gp_X if post==1 & d_pre_CVC==0 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD8_PanelA.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(All-cause Mortality)


** Detection
****************************

sum death if post==1 & any==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & any==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & any==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & any==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableD8_PanelB.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(All-cause Mortality)



sum death_CVC if post==1 & d_pre_CVC==1 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if post==1 & low_ses==1 & d_pre_CVC==1 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help3==0 & d_pre_CVC==1 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC help3 low_ses interaction2 $patient_X $gp_X if post==1 & d_pre_CVC==1 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD8_PanelB.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(All-cause Mortality)

